using System.Collections;
using UnityEngine;

public class SnowController : MonoBehaviour
{
	public float upOffset = 15f;

	public float frontOffset = 25f;

	private float playerSpeed;

	private Vector3 lastPlayerPosition = Vector3.zero;

	private void Start()
	{
		base.transform.SetParent(null);
	}

	private void OnEnable()
	{
		StartCoroutine(SpeedIEnumerator());
	}

	private void OnDisable()
	{
		StopAllCoroutines();
	}

	private void Update()
	{
		if (!(GameController.instance.playerScript != null) || !(CameraModeControl.instance != null))
		{
			return;
		}
		Vector3 position = new Vector3(CameraModeControl.instance.transform.position.x, 0f, CameraModeControl.instance.transform.position.z);
		if (playerSpeed > 0.1f)
		{
			if (playerSpeed > 3f)
			{
				position += CameraModeControl.instance.transform.forward.normalized * (frontOffset * 1.8f);
			}
			else
			{
				position += CameraModeControl.instance.transform.forward.normalized * frontOffset;
			}
		}
		position.y = GameController.instance.playerScript.transform.position.y + upOffset;
		base.transform.position = position;
		base.transform.rotation = Quaternion.Euler(90f, 0f, 0f);
	}

	private void GetSpeed()
	{
		if ((bool)GameController.instance && (bool)GameController.instance.playerScript)
		{
			playerSpeed = Vector3.SqrMagnitude(lastPlayerPosition - GameController.instance.playerScript.transform.position);
			lastPlayerPosition = GameController.instance.playerScript.transform.position;
		}
	}

	private IEnumerator SpeedIEnumerator()
	{
		while (true)
		{
			GetSpeed();
			yield return new WaitForSeconds(0.1f);
		}
	}
}
